package com.nuoniu.sibanyun.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.entity.SysCompany;
import com.nuoniu.sibanyun.entity.crm.CrmSecretKey;
import com.nuoniu.sibanyun.entity.dto.SysCompanyDto;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公司表 Mapper 接口
 * </p>
 *
 * @author baiqiang
 * @since 2020-04-23
 */
@Component
@Mapper
public interface SysCompanyMapper extends BaseMapper<SysCompany> {

    /**
     * 根据公司名称获取
     * @author 小啊刚
     * @date 2020/6/10
     */
    @Select("select * from sys_tenant where company_name = #{companyName} limit 1")
    SysCompany getByName(String companyName);

    /**
     * 公司详情
     * @author 小啊刚
     * @date 2020-07-22
     */
    @Select("select id, company_name companyName, secret_key secretKey, secret_key_id secretKeyId, company_user companyUser, free_user freeUser, company_code companyCode " +
            "from sys_tenant where id = #{companyId}")
    Map getCompanyInfo(Integer companyId);

    /**
     * 分页获取公司列表
     * @author 小啊刚
     * @date 2020-07-24
     */
//    @Select("select c.*, (select count(1) from sys_user where company_id = c.id)enterpriseScale from sys_tenant c")
    Page<SysCompany> pageList(Page page, @Param("company") SysCompany company);

    /**
     * 修改租户免费用户数
     * @author 小啊刚
     * @date 2020-07-30
     */
    @Update("update sys_tenant set free_user = #{freeUsers}")
    void updateFreeUsers(Integer freeUsers);

    /**
     * 获取使用该密钥的公司
     * @author 小啊刚
     * @date 2020-08-03
     */
    @Select("select * from sys_tenant where secret_key = #{secretKey} ")
    List<SysCompany> getBySecretKye(CrmSecretKey secretKey);

    /**
     * 根据code,id获取公司
     * @author 小啊刚
     * @date 2020-08-03
     */
    @Select("select id, company_name companyName from sys_tenant where id = #{cId} and company_code = #{code}")
    Map<String, String> getEntryCompany(String cId, String code);

    /**
     * 获取所有租户
     * @author 小啊刚
     * @date 2020-08-07
     */
    @Select("select * from sys_tenant")
    List<SysCompany> getAll();

    @Select("select * from sys_tenant where id=#{companyId}")
    SysCompanyDto getSysCompany(Integer companyId);
}
